#include "ws/WsPelicanoCControllerBinding.nsmap"

#include "pelicanor.h"
#include <QtGui>
#include <QListIterator>

PelicanoR::PelicanoR(QWidget *parent) :
    QDialog(parent)
{
    lblTitle= new QLabel;
    lblStatus= new QLabel;
    leTitle= new QLineEdit;
    quitButton = new QPushButton(tr("Salir"));
    startButton = new QPushButton(tr("Trabajando"));
    startButton->setEnabled(false);
    progressBar = new QProgressBar();
    progressBar->setRange(0,100);
    lblTitle->setText(tr("Titulo"));
    cmbRegions = new QComboBox;

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);
    buttonLayout->addWidget(quitButton);
    buttonLayout->addWidget(startButton);
    buttonLayout->addStretch(1);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(lblTitle);
    mainLayout->addWidget(leTitle);
    mainLayout->addWidget(cmbRegions);
    mainLayout->addWidget(progressBar);
    mainLayout->addWidget(lblStatus);
    mainLayout->addLayout(buttonLayout);
    setLayout(mainLayout);

    setWindowTitle(tr("Pelicano Ripper"));
    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    Settings *settings;
    CurrentStatus *currentStatus = CurrentStatus::getInstance();
    do
    {
        settings = Settings::getInstance();
    }
    while(currentStatus->getCurrentStatus()==CurrentStatus::CURRENT_STATUS_CONFIGURING);

    CFGFile config;
    _processName =config.getProcessName();
    if(_processName=="")
    {
        PelicanoSWS _pelicanosws;
        config.writeValue("processName",_pelicanosws.getProcessName());
        config.load();
        _processName =config.getProcessName();
    }
    Logger::log()<<"======================";
    Logger::log()<<"Finished configuration";
    Logger::log()<<"======================";
    qDebug()<<"======================";
    qDebug()<<"Finished configuration";
    qDebug()<<"======================";
    copierHandler = new CopierHandler(config.getDrive(),"/",config.getFinalPath(),config.getFinalPath(),_processName);
    //api = new MyMovieApi(QString(""),true,true,0);

    Logger::log()<<"driveWatcher: "<<config.getDrive();
    driveWatcher= new DriveWatcher(config.getDrive());
    discIdentifier= new DiscIdentifier(config.getDrive());
    //_anydvdHandler = new AnydvdHandler;

    connect(settings,SIGNAL(changedFinalPath(QString)),copierHandler,SLOT(changeFinalFolder(QString)));

    connect(settings,SIGNAL(changedTempPath(QString)),copierHandler,SLOT(changeTempFolder(QString)));

    connect(settings,SIGNAL(changedDriveLetter(QString)),driveWatcher,SLOT(changeLetter(QString)));

    connect(settings,SIGNAL(changedDriveLetter(QString)),discIdentifier,SLOT(changeLetter(QString)));

    connect(settings,SIGNAL(changedDriveLetter(QString)),copierHandler,SLOT(changeLetter(QString)));

    QObject::connect(copierHandler, SIGNAL(updateProgress(int)),
            this, SLOT(updateProgress(int)));

    QObject::connect(driveWatcher, SIGNAL(discDetected()),
            this, SLOT(discDetected()));

    QObject::connect(discIdentifier, SIGNAL(discIdentified(QByteArray)),
            this, SLOT(discIdentified(QByteArray)));

    QObject::connect(discIdentifier, SIGNAL(reloadDisc()),
            this, SLOT(reloadDisc()));

    QObject::connect(copierHandler, SIGNAL(step()),
            this, SLOT(step()));

//    QObject::connect(copierHandler, SIGNAL(readyToCopy()),
//            this, SLOT(copyDisc()));

//    QObject::connect(copierHandler, SIGNAL(readyToCopy()),
//            this, SLOT(setStartButtonEnabled()));

    QObject::connect(copierHandler, SIGNAL(finished(QString,QString,QString,bool)),
            this, SLOT(copyDone(QString,QString,QString,bool)));
}
void PelicanoR::step()
{
    Logger::log()<<"PelicanoR::step()";
    PelicanoSWS _pelicanosws;
    StepResponse stepResponse= _pelicanosws.getNextStep(_processName);
    _currentStep = (eSteps)stepResponse.step.toInt();
    copierHandler->setFileName(stepResponse.file_name);
    copierHandler->setIdWs(stepResponse.id_auto_ripper);
    CFGFile config;
    //QString path = config.getFinalPath()+"/"+stepResponse.file_name+"/";
    QString path = config.getFinalPath();
    copierHandler->setDestinationPath(path);

    switch(_currentStep)
    {
    case STEP_MAKE_MKV:
        _currentStep = STEP_MAKE_MKV;
        Logger::log()<<"PelicanoR::step(): STEP_MAKE_MKV";
        copierHandler->startMakeMKV();
        break;
    case STEP_IDLE:
        _currentStep = STEP_IDLE;
        Logger::log()<<"PelicanoR::step(): STEP_IDLE";
       break;
    case STEP_START:
        _currentStep = STEP_START;
        Logger::log()<<"PelicanoR::step(): STEP_START";
        if(!driveWatcher->isRunning())
        {
            driveWatcher->start();
            discIdentifier->moveToThread(driveWatcher);
        }
       break;
    case STEP_7ZIP:
        _currentStep = STEP_7ZIP;
        Logger::log()<<"PelicanoR::step(): STEP_7ZIP";
        copierHandler->start7ZIP();
       break;
    case STEP_WINRAR:
        _currentStep = STEP_WINRAR;        
        Logger::log()<<"PelicanoR::step(): STEP_WINRAR";
        copierHandler->startRAR();
        break;
    case STEP_PAR2:
        _currentStep = STEP_PAR2;
        Logger::log()<<"PelicanoR::step(): STEP_PAR2";
        copierHandler->startPAR2();
        break;
    case STEP_YENC:
        _currentStep = STEP_YENC;
        Logger::log()<<"PelicanoR::step(): STEP_YENC";
        copierHandler->startYENC();
        break;
    case STEP_REYENC:
        _currentStep = STEP_REYENC;
        Logger::log()<<"PelicanoR::step(): STEP_REYENC";
        copierHandler->startYENC(true);
        break;
    case STEP_UPLOADING_NZB:
        _currentStep = STEP_UPLOADING_NZB;
        Logger::log()<<"PelicanoR::step(): STEP_UPLOADING_NZB";
        copierHandler->startUPLOADNZB();
        break;
    case STEP_CLEAN_FOLDER:
        _currentStep = STEP_CLEAN_FOLDER;
        Logger::log()<<"PelicanoR::step(): STEP_CLEAN_FOLDER";
        copierHandler->cleanFolder(copierHandler->getDestinationPath());
        break;
    case STEP_EJECT_DISC:
        _currentStep = STEP_EJECT_DISC;
        Logger::log()<<"PelicanoR::step(): STEP_EJECT_DISC";
        copierHandler->ejectDisc();
        break;
    default:
        Logger::log()<<"PelicanoR::step(): default: "<<_currentStep;
     break;
    }
}

void PelicanoR::copyDone(QString id, QString path,QString discId,bool parentalControl)
{
    CurrentStatus * status = CurrentStatus::getInstance();
    status->setRipping(false);

    //api->sendNewRippedDisc(id,path,discId,parentalControl);
    cmbRegions->clear();
    leTitle->clear();
}
void PelicanoR::reloadDisc()
{
    driveWatcher->reloadDrive();
}

void PelicanoR::discIdentified(QByteArray id)
{
    qDebug()<<"PelicanoR::discIdentified(QByteArray id):"<<id;
    Logger::log()<<"PelicanoR::discIdentified(QByteArray id):"<<id;
    DiscData discData;
    discData.discId = id;
    discData.discname = id;
    copierHandler->setDiscData(discData);

    if(copierHandler->getIdWs()!="0"&&driveWatcher->isRunning())
    {
        qDebug()<<"driveWatcher->terminate()";
        driveWatcher->terminate();
        driveWatcher->reloadDrive();
        //discIdentifier->moveToThread(driveWatcher);
    }

}

void PelicanoR::discDetected()
{
    DriveWatcher *dWatcher = (DriveWatcher*)sender();
    copierHandler->setLabel(dWatcher->getLabel());
    discIdentifier->identify();
}

void PelicanoR::updateProgress(int progress)
{
    progressBar->setValue(progress);
}

void PelicanoR::start()
{
    //_anydvdHandler->start();
    this->step();
}

void PelicanoR::setStartButtonEnabled()
{
    qDebug()<<"PelicanoR::setStartButtonEnabled()";

    startButton->setEnabled(true);
}

void PelicanoR::copyDisc()
{
    CurrentStatus * status = CurrentStatus::getInstance();
    status->setRipping(true);
    //copierHandler->startCopyDisc();
}
PelicanoR::~PelicanoR()
{
    delete lblTitle;
    delete leTitle;
    delete progressBar;
    delete quitButton;
    delete startButton;
    delete cmbRegions;
//    delete _anydvdHandler;
//    delete copierHandler;
//    delete api;
//    delete driveWatcher;
//    delete discIdentifier;
}
